package it.gommonauti.quiz.patente.app;

import it.gommonauti.patenti.dao.MainQuestionDAO;
import it.gommonauti.patenti.dao.factory.DAOFactory;
import it.gommonauti.patenti.entity.CONSTANTS;
import it.gommonauti.patenti.entity.Category;
import it.gommonauti.patenti.entity.Exam;
import it.gommonauti.patenti.entity.MainQuestion;
import it.gommonauti.patenti.exception.ApplicationException;

import java.sql.SQLException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

/**
 * 
 * @author Francesco Stallone ( f.stallone73@gmail.com ).
 *
 * Base activity.
 */

public abstract class BaseActivity extends Activity implements CONSTANTS {

	/**
	 * Shared preferences.
	 */
	private SharedPreferences sharedPreferences;
	
	/**
	 * Shared preferences editor.
	 */
	private SharedPreferences.Editor editor;
	
	/**
	 * Main question DAO.
	 */
	private MainQuestionDAO mainQuestionDAO;
	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		Log.i(getDebugTag(), "onCreate");
		
		sharedPreferences = getSharedPreferences(sharedPreferencesName, Context.MODE_PRIVATE);
		
		editor = sharedPreferences.edit();
		
		// create the required DAO Factory
		DAOFactory h2dbFactory = DAOFactory.getDAOFactory(DAOFactory.H2_DB);
		
		// Create a DAO
		mainQuestionDAO = h2dbFactory.getMainQuestionDAO();
	}
	
	/**
	 * Get selected exam ID.
	 * @return
	 */
	protected int getSelectedExamID(){
	
		return sharedPreferences.getInt(idExamKey, -1);
	
	}
	
	/**
	 * Get selected language ID.
	 * @return
	 */
	protected String getSelectedLanguageID(){
		
		return sharedPreferences.getString(idLanguageKey, "");
	
	}
	
	/**
	 * Get selected category ID.
	 * @return
	 */
	protected int getSelectedCategoryID(){
		
		return sharedPreferences.getInt(idCategoryKey, -1);
		
	}
	
	/**
	 * Set selected exam ID.
	 */
	protected void setSelectedExamID(int value){
		
		editor.putInt(idExamKey, value);
		editor.commit();
	
	}
	
	/**
	 * Set selected language ID.
	 */
	protected void setSelectedLanguageID(String value){
		
		editor.putString(idLanguageKey, value);
		editor.commit();
		
	}
	
	/**
	 * Set selected category ID.
	 */
	protected void setSelectedCategoryID(int value){
		
		editor.putInt(idCategoryKey, value);
		editor.commit();
		
	}

	protected ArrayList<Category> getCategories(String idLanguage, int idExam)  {

		ArrayList<Category> categories = null;
		
		try{


			// create the required DAO Factory
			DAOFactory h2dbFactory =   
					DAOFactory.getDAOFactory(DAOFactory.H2_DB);

			// Create a DAO
			MainQuestionDAO mainQuestionDAO = h2dbFactory.getMainQuestionDAO();

			categories = mainQuestionDAO.getCategories(idLanguage, idExam);

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ApplicationException e) {
			System.out.println(e.getTypes());
		}
		return categories;
	}	
	
	protected MainQuestion getMainQuestion(int idMainQuestion, String idLanguge)  {

		MainQuestion mainQuestion = null;

		try{
			
			mainQuestion = mainQuestionDAO.getMainQuestion(idMainQuestion, idLanguge);

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ApplicationException e) {
			System.out.println(e.getTypes());
		}
		return mainQuestion;
	}

	protected int[] getMainQuestionsIDLinear(String idLanguage, int idExam)  {

		int[] mainQuestions = null;

		try{

			mainQuestions = mainQuestionDAO.getMainQuestionsIDLinear(idLanguage, idExam);

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ApplicationException e) {
			System.out.println(e.getTypes());
		}
		return mainQuestions;

	}	
	
	protected int[] getMainQuestionsIDRandom(String idLanguage, int idExam)  {

		int[] mainQuestions = null;

		try{

			mainQuestions = mainQuestionDAO.getMainQuestionsIDRandom(idLanguage, idExam);

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ApplicationException e) {
			System.out.println(e.getTypes());
		}
		return mainQuestions;

	}	

	/**
	 * Get exam by ID an Language.
	 */
	protected Exam getExam(int id, String idLanguage)  {
		
		Exam exam = null;
		
		try{

			exam = mainQuestionDAO.getExam(id, idLanguage);

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ApplicationException e) {
			System.out.println(e.getTypes());
		}
		
		return exam;
		
	}
	
	protected int[] getMainQuestionsIDRandomByIDCategoryAndIDLanguage(int idCategory, String idLanguage)  {

		int[] mainQuestions = null;

		try{

			mainQuestions = mainQuestionDAO.getMainQuestionsIDRandomByIDCategoryAndIDLanguage(idCategory, idLanguage);

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ApplicationException e) {
			System.out.println(e.getTypes());
		}
		return mainQuestions;

	}	
	
	/**
	 * Return the activity debug tag for logging. 
	 * @return
	 */
	protected abstract String getDebugTag();

	
	protected void setQuizStartIndex(int value){
		
		editor.putInt(idQuizStartIndex, value);
		editor.commit();
		
	}
	
	public int getQuizStartIndex(){
		return sharedPreferences.getInt(idQuizStartIndex, 0);
	}
	
	public int getQuizCount(){
		return sharedPreferences.getInt(idQuizCount, 20);
	}
	
	protected void setQuizCount(int value){
		
		editor.putInt(idQuizCount, value);
		editor.commit();
		
	}
	
}
